var params = {};
var defaultPageSize = 10;
var currentPage = 1;
var pageSize = defaultPageSize;

var option = {};

$(function(){
	setModuleOption();
	loadGrid(option);
	
	$('.check').on('click',search);
	$('.empty').on('click',emptySearch);
});

function loadGrid(option){
	params.currentPage = currentPage;
	params.pageSize = pageSize;
	$.ajax({
		url: option.url,
		type:'post',
		data:params
	}).done(function(data){
		var result = data.bo;
		var pager = {};
		option.target.datagrid({
			fitColumns:true,
			singleSelect:true,
			pagination:true,
			remoteSort:false,
			multiSort:true,
			loadMsg:"数据加载中，请稍后……",
			data:result.list,
			columns:[option.columns],
			onSortColumn: function (sort, order) {
				setPager(pager,result.total);
	        }
		});
		
		pager = option.target.datagrid().datagrid('getPager');	// get the pager of datagrid
		setPager(pager,result.total);
	});
}

function setPager(pager,total){
	pager.pagination({
		total:total,
		pageNumber:currentPage,
	    pageSize:defaultPageSize,
	    layout:['first','prev','links','next','last'],
	    onSelectPage:function(cpage, psize){
	    	currentPage=cpage;
	    	pageSize=psize;
	    	loadGrid(option);
		}
	});
}

function setModuleOption(){
	option = {};
	option.url = webUrl+'sysPermission/pageModule';
	option.target = $('#modulesGrid');
	option.columns = [
			          {field:'_itemNo',title:'序号',align:'center',width:50,formatter:formatId},
			          {field:'module',title:'模块名称',width:150,sortable:true},
			          {field:'opers',title:'模块操作',width:150,sortable:true}
			      ];
}

var formatId = function(val, row,index){
	return (index+1)+(currentPage-1)*pageSize;
}

function search(){
	var keywords = $('#keywords').val();
	if(keywords!='')
	{
		currentPage = 1;
		pageSize = defaultPageSize;
		params.modulename = keywords;
		loadGrid(option);
	}
}

function emptySearch(){
	$('#keywords').val('');
	params = {};
	loadGrid(option);
}